
$(function(){
	
		//查询
		baseRead(isRead);
		//设置选中 效果messageUl
		$("#messageUl").find("a").each(function(){
			if($(this).attr("key")==isRead){			
				$(this).parent("li").addClass("active");
			}
		});
		//触发未读和已读
		$("#messageUl").on("click","a",function(){
			queryRead($(this).attr("key"));
		});
		selectMessageApp(uuid);
		//查看信息
		$("#messageTable").on("click",".check",function(){
			
			selectMessageApp($(this).attr("key"));
		});
		
	
});




/**
 * 查询未读的消息
 */
function baseRead(isRead){
	$("#isRead").val(isRead);
	var readTableColums = [//定义列
	                      {
	          				"mData":"uuid",
	          				"sClass": "center",
	          				"bSortable": false,
	          				"mRender" : function(data, type, row) {
	          					
	          					var viewLogo = '<a class="blue check" href="#" key=\''+data+'\'><i class="icon-eye-open bigger-130"></i></a>';
	          					var deleteLogo = '<a class="red" id="delMessage" title="删除消息" href="javascript:delMessage(\''+data+'\')"><i class="icon-trash bigger-130"></i></a>';
	          					
	          						return '<div class="action-buttons ">'+viewLogo+'<span class="vbar"></span>'+deleteLogo+' </div>';
	          					
	          				}
	                      },
	                      {"mData":"msgTitle"},         	                                  
	                      {
	                    	  "mData": function(data, type, row) {
		          					return DateUtil.dateFormat(data.msgCrateDt);
	                    	  } 
	                      },
	                      {
		                    	
	                    	  "mData": function(data, type, row) {
	          					return DateUtil.dateFormat(data.msgReadDt);
	          				}
	                      }
	              ];	          	
	              //初始化dataTable
	          	datatable = $('#messageTable').dataTable({	          		
	              	'sAjaxSource': ctx+'/sys/amcm/queryPageRead.do',	//请求数据源 
	              	"fnServerData": function(sSource, aoData, fnCallback,oSettings){
	              		$.extend(aoData,[{name:"isRead",value:$("#isRead").val()}]);
	              	
	            		$.ajax({
	        	    		dataType: 'json',
	        	    		url : sSource,
	        	    		data: aoData,
	        	    		success: fnCallback
	            		});		
	              	},
	          		'bServerSide':true,							//是否启动服务器端数据导入
	          		"bLengthChange" : false,
	          		"bInfo" : true,
	          		"bPaginate" : true,
                  	'bFilter': false,                       	//是否使用内置的过滤功能。
                  	'aoColumns': readTableColums,
                 	"sDom": '<"toolbar">frtip', 
                   	"oLanguage": oLanguage
	          	});             
}
/**
 * 通过uuid查看消息
 */
function selectMessageApp(uuid){
	if(uuid!=""){
		Otod.ajax({			   
			url:ctx+'/sys/amcm/getAmcmMsgApp.do',
			data:{uuid:uuid},
			type:'GET',
			successFn:function(data){
				queryRead($("#isRead").val());
				readerMsgInfo(data);
			}
			
		});
	}
	
}

//查询
function queryRead(isRead) {
	$("#isRead").val(isRead);
	var oSettings = datatable.fnSettings();
	oSettings._iDisplayStart = 0;
	datatable.fnClearTable(0); //清空数据
	datatable.fnDraw(); //重新加载数据 
}
/**
 * 加载消息内容的模板
 */
function readerMsgInfo(data){
	
	var showMsgInfo=$("#showMegInfo").html();
	Mustache.parse(showMsgInfo);
	var readered = Mustache.render(showMsgInfo,{
		data:data,
		formatDt:function(){
			return function(text,render){
				return DateUtil.dateFormat(+render(text));
			};
		}
	});
	$("#msgInfoUl").html(readered);
}

/**
 * 
 *删除消息 
 * 
 */
function delMessage(data){
	
	Otod.ajax({			   
		url:ctx+'/sys/amcm/deleteById.do',
		data:{uuid:data},
		type:'GET',
		successFn:function(data){
			queryRead($("#isRead").val());
			readerMsgInfo("");
		}
		
	});
}

